﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

namespace KC.Storage.BlobService
{
    internal partial class AzureReader
    {
        private bool DoesContainerExist(CloudBlobContainer container)
        {
            try
            {
                container.FetchAttributesAsync();
                return true;
            }
            catch (StorageException)
            {
                throw;
            }
        }
        
        public override List<string> ListContainers()
        {
            List<string> containerNames = new List<string>();
            BlobContinuationToken continuationToken = null;

            do
            {
                // Retrieve a segment (up to 1,000 entities).
                var result = this.Client.ListContainersSegmentedAsync(new BlobContinuationToken()).Result;
                foreach (var listBlobItem in result.Results)
                {
                    var url = listBlobItem.Uri.AbsolutePath.Substring(listBlobItem.Uri.AbsolutePath.LastIndexOf("/") + 1);
                    containerNames.Add(listBlobItem.Name);
                }

                // Loop until a null continuation token is received, indicating the end of the table.
            } while (continuationToken != null);

            return containerNames;
        }

        public override bool ExistContainer(string containerName)
        {
            if (string.IsNullOrEmpty(containerName))
                return false;

            CloudBlobContainer container = this.Client.GetContainerReference(containerName);
            return DoesContainerExist(container);
        }

        public override List<string> ListBlobIds(string containerName)
        {
            if (string.IsNullOrEmpty(containerName)) return new List<string>();

            CloudBlobContainer container = this.Client.GetContainerReference(containerName);

            List<string> blobIds = new List<string>();
            BlobContinuationToken continuationToken = null;

            do
            {
                // Retrieve a segment (up to 1,000 entities).
                var result = container.ListBlobsSegmentedAsync(new BlobContinuationToken()).Result;
                foreach (var listBlobItem in result.Results)
                {
                    blobIds.Add(listBlobItem.Uri.AbsolutePath.Substring(listBlobItem.Uri.AbsolutePath.LastIndexOf("/") + 1));
                }

                // Loop until a null continuation token is received, indicating the end of the table.
            } while (continuationToken != null);

            return blobIds;
        }

        public override List<string> ListBlobIdsWithMetadata(string containerName)
        {
            if (string.IsNullOrEmpty(containerName)) return new List<string>();

            CloudBlobContainer container = this.Client.GetContainerReference(containerName);

            List<string> blobIds = new List<string>();
            BlobContinuationToken continuationToken = null;

            do
            {
                // Retrieve a segment (up to 1,000 entities).
                var result = container.ListBlobsSegmentedAsync(new BlobContinuationToken()).Result;
                foreach (var listBlobItem in result.Results)
                {
                    blobIds.Add(listBlobItem.Uri.AbsolutePath.Substring(listBlobItem.Uri.AbsolutePath.LastIndexOf("/") + 1));
                }

                // Loop until a null continuation token is received, indicating the end of the table.
            } while (continuationToken != null);

            return blobIds;
        }
    }
}
